package kr.co.happenstance;


import kr.co.happenstance.util.AndroidUtil;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.Animation.AnimationListener;
import android.widget.FrameLayout;
import android.widget.ImageView;

public class SettingActivity extends BaseActivity implements View.OnClickListener, View.OnTouchListener, AnimationListener{
	protected final String TAG = "<<<SettingActivity>>>";
	
	private int curSound = C.SETTING_SOUND_ON;
    /**
     * 왼쪽 애니메이션 정의 상수
     */
    private final int TRANLATE_LEFT = 0;
    
    /**
     * 오른쪽 애니메이션 정의 상수
     */
    private final int TRANLATE_RIGHT = 1;
    
    /**
     * 애니메이션 효과 없을때 정의 상수
     */
    private final int TRANLATE_NOT = -1;  
    
	/**
	 * 알림소리설정 버튼 FrameLayout 에서 사용자가 터치한(ACTION_DOWN) 시작 좌표 
	 */
	private float touchDownX =0;
  
	/**
	 * 애니메이션 실행 여부
	 */
	private boolean isAnimationStarted;	
	
	/**
	 * 알림소리설정  버튼 FrameLayout
	 */
	private FrameLayout flSound;
	
	/**
	 * 알림소리설정 버튼 FrameLayout 에서 뒤로 숨겨질 ImageView
	 */
	private ImageView ivSettingSoundOff;
	
	/**
	 * 알림소리설정 버튼 FrameLayout 에서 앞으로 보여질 ImageView
	 */
	private ImageView ivSettingSoundOn;	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_setting);
		
		flSound = (FrameLayout) this.findViewById(R.id.fl_sound);
		flSound.setOnTouchListener(this);
		
		ivSettingSoundOff = (ImageView) this.findViewById(R.id.iv_setting_sound_off);
		ivSettingSoundOn = (ImageView) this.findViewById(R.id.iv_setting_sound_on);		
		
		String tmpCurSound = AndroidUtil.getAppPreferences(this, C.SHARED_PREF_NAME, C.SHARED_PREF_KEY_SOUND);
		if(tmpCurSound.equals("")){
			curSound = C.SETTING_SOUND_ON;
		}else{
			curSound = Integer.parseInt(tmpCurSound);
		}
		
		//초기 소리설정값  셋팅
		setSoundButton(curSound, TRANLATE_NOT);
		
		init(this, C.MENU_ID_SETTING);
	}

	@Override
	public void onClick(View v) {
		Intent intent = null;
		switch(v.getId()){
			case R.id.ll_notice:
				intent = new Intent(this, NoticeActivity.class);
				intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				startActivity(intent);	
				
				break;
			case R.id.ll_paylog:
				intent = new Intent(this, PaylogActivity.class);
				intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				startActivity(intent);				
				
				break;
			case R.id.ll_help:
				new CustomAlertDialogBuilder(this, R.string.alert_title_help, R.string.alert_msg_help).create();

				break;
			case R.id.ll_gps:
				new CustomAlertDialogBuilder(this)
				.setTitle(R.string.alert_title_gps)
				.setMessage(R.string.alert_msg_gps)
				.setCancelable(true)
				.setPositiveButton(R.string.confirm, new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int arg1) {
						dialog.dismiss();
						
						//GPS 설정 호출
						Intent intent = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
						intent.addCategory(Intent.CATEGORY_DEFAULT); 
						startActivity(intent);
					}
				})
				.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
					}
				})
				.create();					
				
				break;
		}
		
	}

	@Override
	public void onAnimationStart(Animation animation) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onAnimationEnd(Animation animation) {
		isAnimationStarted =  false;
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onAnimationRepeat(Animation animation) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		final int action = event.getAction();
		
		if(!isAnimationStarted){
			switch(action){
				case MotionEvent.ACTION_DOWN:
					this.touchDownX = event.getX();
					break;

				case MotionEvent.ACTION_UP:
					final float x = event.getX();
					float dx = x - this.touchDownX ;
					Log.d(TAG, "dx["+dx+"]");
					if(dx > 10 && curSound == C.SETTING_SOUND_OFF){
						setSoundButton(C.SETTING_SOUND_ON, TRANLATE_RIGHT);
						this.touchDownX = 0;
						
					}else if(dx < -10 && curSound == C.SETTING_SOUND_ON){
						setSoundButton(C.SETTING_SOUND_OFF, TRANLATE_LEFT);
					}
					break;
			}//switch
		}//if
		return true;
	}
	
	private void setSoundButton(int settingSound,  int tranlate){
		Log.d(TAG, "setSoundButton................");
		
		//현재 보여질 소리설정 Bitmap
		Bitmap sound_bitmap = null;
		if(settingSound == C.SETTING_SOUND_OFF){
			sound_bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.setting_sound_off);
		}else{
			sound_bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.setting_sound_on);
		}	
		
		//나가는(숨겨지는) 애니메이션
		Animation anim_out = null;
		
		//들어오는(보이는) 애니메이션
		Animation anim_in = null;
		
		switch(tranlate){
			case TRANLATE_RIGHT:
				anim_out =  AnimationUtils.loadAnimation(this, R.anim.translate_right_out);
				anim_in = AnimationUtils.loadAnimation(this, R.anim.translate_right_in);

				//리스너는 들어오는 Animation에만 등록
				anim_in.setAnimationListener(this);
				
				isAnimationStarted = true;
				
				ivSettingSoundOff.setImageDrawable(ivSettingSoundOn.getDrawable());
				ivSettingSoundOff.startAnimation(anim_out);
				
				ivSettingSoundOn.setImageBitmap(sound_bitmap);
				ivSettingSoundOn.startAnimation(anim_in);
				break;
				
			case TRANLATE_LEFT:
				anim_out =  AnimationUtils.loadAnimation(this, R.anim.translate_left_out);
				anim_in = AnimationUtils.loadAnimation(this, R.anim.translate_left_in);
				
				//리스너는 들어오는 Animation에만 등록
				anim_in.setAnimationListener(this);

				isAnimationStarted = true;
				
				ivSettingSoundOff.setImageDrawable(ivSettingSoundOn.getDrawable());
				ivSettingSoundOff.startAnimation(anim_out);
				
				ivSettingSoundOn.setImageBitmap(sound_bitmap);
				ivSettingSoundOn.startAnimation(anim_in);
				break;
				
			case TRANLATE_NOT:
				ivSettingSoundOn.setImageBitmap(sound_bitmap);
				break;
				
		}//switch
		this.curSound = settingSound;
		Log.d(TAG, "curSound["+curSound+"]");
		AndroidUtil.setAppPreferences(this, C.SHARED_PREF_NAME, C.SHARED_PREF_KEY_SOUND, Integer.toString(this.curSound));
		
	}	
	
}
